package com.wang.jmonkey.modules.system.api;

import com.wang.jmonkey.common.annotation.Log;
import com.wang.jmonkey.common.result.HttpResult;
import com.wang.jmonkey.modules.system.model.entity.SystemAclEntity;
import com.wang.jmonkey.modules.system.model.enums.SystemAclPrincipalTypeEnum;
import com.wang.jmonkey.modules.system.model.param.SystemAclAuthParam;
import com.wang.jmonkey.modules.system.service.SystemAclService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资源权限信息表(SystemAcl)表控制层
 *
 * @author HeJiawang
 * @since 2021-05-21 21:43:56
 */
@RestController
@RequestMapping("/acl")
public class SystemAclApi {

    /**
     * 服务对象
     */
    @Autowired
    private SystemAclService service;

    /**
     * 为用户授权
     * @param aclAuthParam 授权信息
     * @return true 授权成功
     */
    @Log("用户管理——授权")
    @PostMapping("/auth/user")
    public HttpResult<Void> authUser(@RequestBody @Validated SystemAclAuthParam aclAuthParam) {
        aclAuthParam.setPrincipalType(SystemAclPrincipalTypeEnum.USER);

        return new HttpResult<Void>().setSuccess(service.auth(aclAuthParam));
    }

    /**
     * 为角色授权
     * @param aclAuthParam 授权信息
     * @return true 授权成功
     */
    @Log("角色管理——授权")
    @PostMapping("/auth/role")
    public HttpResult<Void> authRole(@RequestBody @Validated SystemAclAuthParam aclAuthParam) {
        aclAuthParam.setPrincipalType(SystemAclPrincipalTypeEnum.ROLE);

        return new HttpResult<Void>().setSuccess(service.auth(aclAuthParam));
    }

    /**
     * 为机构授权
     * @param aclAuthParam 授权信息
     * @return true 授权成功
     */
    @Log("机构管理——授权")
    @PostMapping("/auth/dept")
    public HttpResult<Void> authDept(@RequestBody @Validated SystemAclAuthParam aclAuthParam) {
        aclAuthParam.setPrincipalType(SystemAclPrincipalTypeEnum.DEPT);

        return new HttpResult<Void>().setSuccess(service.auth(aclAuthParam));
    }

    /**
     * 为职务授权
     * @param aclAuthParam 授权信息
     * @return true 授权成功
     */
    @Log("职务管理——授权")
    @PostMapping("/auth/duty")
    public HttpResult<Void> authDuty(@RequestBody @Validated SystemAclAuthParam aclAuthParam) {
        aclAuthParam.setPrincipalType(SystemAclPrincipalTypeEnum.DUTY);

        return new HttpResult<Void>().setSuccess(service.auth(aclAuthParam));
    }

    /**
     * 为岗位授权
     * @param aclAuthParam 授权信息
     * @return true 授权成功
     */
    @Log("岗位管理——授权")
    @PostMapping("/auth/job")
    public HttpResult<Void> authJob(@RequestBody @Validated SystemAclAuthParam aclAuthParam) {
        aclAuthParam.setPrincipalType(SystemAclPrincipalTypeEnum.JOB);

        return new HttpResult<Void>().setSuccess(service.auth(aclAuthParam));
    }

    /**
     * 获取用户资源权限id集合
     * @param userId 用户id
     * @return 资源id集合
     */
    @GetMapping("/list/user/{userId}")
    public HttpResult<List<Long>> listUserAcl(@PathVariable Long userId) {
        return new HttpResult<>( service.listUserAcl( userId ) );
    }

    /**
     * 获取角色资源权限id集合
     * @param roleId 角色id
     * @return 资源id集合
     */
    @GetMapping("/list/role/{roleId}")
    public HttpResult<List<Long>> listRoleAcl(@PathVariable Long roleId) {
        return new HttpResult<>( service.listAcl(roleId, SystemAclPrincipalTypeEnum.ROLE) );
    }

    /**
     * 获取机构资源权限id集合
     * @param deptId 机构id
     * @return 资源id集合
     */
    @GetMapping("/list/dept/{deptId}")
    public HttpResult<List<Long>> listDeptAcl(@PathVariable Long deptId) {
        return new HttpResult<>( service.listAcl(deptId, SystemAclPrincipalTypeEnum.DEPT) );
    }

    /**
     * 获取职务资源权限id集合
     * @param dutyId 职务id
     * @return 资源id集合
     */
    @GetMapping("/list/duty/{dutyId}")
    public HttpResult<List<Long>> listDutyAcl(@PathVariable Long dutyId) {
        return new HttpResult<>( service.listAcl(dutyId, SystemAclPrincipalTypeEnum.DUTY) );
    }

    /**
     * 获取岗位资源权限id集合
     * @param jobId 岗位id
     * @return 资源id集合
     */
    @GetMapping("/list/job/{jobId}")
    public HttpResult<List<Long>> listJobAcl(@PathVariable Long jobId) {
        return new HttpResult<>( service.listAcl(jobId, SystemAclPrincipalTypeEnum.JOB) );
    }
}
